package com.skp.shaphan.actions;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.SwingUtilities;

import com.skp.job.ProjectJobException;
import com.skp.shaphan.model.ConnectionDetails;
import com.skp.shaphan.sql.SQLPreProcessor;
import com.skp.shaphan.ui.SQLEditor;
import com.skp.shaphan.ui.results.IResultPane;
import com.skp.shaphan.ui.results.ResultPane;

/**
 * Runs a piece of SQL from a SQLEditor
 * @author Stephen Phillips
 *
 */
public class ExecuteSQLAction extends Thread implements Runnable {
	private SQLEditor editor;
	private IResultPane resultPane;
	
	public ExecuteSQLAction() {
		
	}
	
	@Override
	public void run() {
		// Clear the old results
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				resultPane.setStatusMessage("resetting...");
			}
		});
		resultPane.setResultSet(null, null);
		resultPane.clearMessages();
		/*
		ResultSet oldResults = resultPane.getResultSet();
		try {
			if(oldResults != null) {
				oldResults.getStatement().close();
				oldResults.close();
			}
		} catch (SQLException e) {
			(new ProjectJobException(e)).displayException();
		}*/
		editor.markAsRan();
		
		// Getting the new results
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				resultPane.setStatusMessage("loading...");
			}
		});
		// What SQL do we run
		ConnectionDetails cd = editor.getConnection();
		String sqlStatement = editor.getSQL();
		System.out.println("sql = " + sqlStatement);
		SQLPreProcessor spp = cd.getSQLPreprocessor(sqlStatement);
		sqlStatement = spp.process();
		System.out.println("now sql = " + sqlStatement);
		Connection connection = cd.getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sqlStatement, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
		} catch (SQLException e) {
			resultPane.setSQLMessages(null, statement, e);
			return;
		}
		try {
			statement.execute();
		} catch (SQLException e) {
			resultPane.setSQLMessages(null, statement, e);
		}
		ResultSet results = null;
		try {
			results = statement.getResultSet();
		} catch (SQLException e) {
			(new ProjectJobException(e)).displayException();
			return;
		}
		if(results != null) {
			resultPane.setResultSet(sqlStatement, results);
		} else {
			int updateCount = 0;
			try {
				updateCount = statement.getUpdateCount();
			} catch (SQLException e) {
				(new ProjectJobException(e)).displayException();
				return;
			}
			final int finalUpdateCount = updateCount;
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					resultPane.setStatusMessage(finalUpdateCount + " rows updated");
				}
			});
		}
		resultPane.setSQLMessages(results, statement, null);
	}

	public SQLEditor getEditor() {
		return editor;
	}

	public void setEditor(SQLEditor editor) {
		this.editor = editor;
	}

	public IResultPane getResultPane() {
		return resultPane;
	}

	public void setResultPane(IResultPane iResultPane) {
		this.resultPane = iResultPane;
	}

}
